"""The application's model objects"""
import sqlalchemy as sa
import datetime
from sqlalchemy import orm

from pglite.model import meta
from sqlalchemy.orm import relation, backref
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, Integer, String, DateTime, MetaData, ForeignKey

def init_model(engine):
    """Call me before using any of the tables or classes in the model"""
    ## Reflected tables must be defined and mapped here
    #global reflected_table
    #reflected_table = sa.Table("Reflected", meta.metadata, autoload=True,
    #                           autoload_with=engine)
    #orm.mapper(Reflected, reflected_table)
    #
    meta.Session.configure(bind=engine)
    meta.engine = engine
    
Base = declarative_base(metadata=meta.metadata)

# Servers
class Server(Base):
    __tablename__ = 'servers'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    zone = Column(String)
    
    def __init__(self, name, zone):
        self.name = name
        self.zone = zone

# Raid
class Raid(Base):
    __tablename__ = 'raids'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    display_order = Column(Integer)
    default_state = Column(Integer)
    
    def __init__(self, name, display_order, default_state):
        self.name = name
        self.display_order = display_order
        self.default_state = default_state
        
# Achievements
class Achievement(Base):
    __tablename__ = 'achievements'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    type = Column(String)
    value = Column(Integer)
    raid_id = Column(Integer, ForeignKey('raids.id'))
    display_order = Column(Integer)
    
    raid = relation(Raid, backref=backref('achievements', order_by=display_order))
    
    def __init__(self, name, type, value, display_order):
        self.name = name
        self.type = type
        self.value = value
        self.display_order = display_order
        
# Guilds 
class Guild(Base):
    __tablename__ = 'guilds'
    
    id = Column(Integer, primary_key=True)
    name = Column(String)
    server_id = Column(Integer, ForeignKey('servers.id'))
    faction = Column(String)
    status = Column(Integer)
    website_url = Column(String)
    description = Column(String)
    contacts = Column(String)
    score = Column(Integer)
    
    server = relation(Server, backref=backref('guilds', order_by=id))
    
    def __init__(self, name, faction, status=0, website_url="", description="", contacts="", score=0):
        self.name = name
        self.faction = faction
        self.status = status
        self.website_url = website_url
        self.description = description
        self.contacts = contacts
        self.score = score

# Guild Achievements        
class GuildAchievement(Base):
    __tablename__ = 'guild_achievements'
    
    guild_id = Column(Integer, ForeignKey('guilds.id'), primary_key=True)
    achievement_id = Column(Integer, ForeignKey('achievements.id'), primary_key=True)
    date = Column(DateTime, default=datetime.datetime.now)
    mode = Column(String, primary_key=True)
    
    guild = relation(Guild, backref=backref('achievements', order_by=date))
    achievement = relation(Achievement, backref=backref('realizations', order_by=date))
    
    def __init__(self, date, mode):
        self.date = date
        self.mode = mode
        
# Users
class User(Base):
    __tablename__ = 'users'
    
    id = Column(Integer, primary_key=True)
    login = Column(String, unique=True)
    password = Column(String)
    email = Column(String)
    validation_key = Column(String)
    type = Column(String)
    guild_id = Column(Integer, ForeignKey('guilds.id'))
    
    guild = relation(Guild, backref=backref('owner'))
    
    def __init__(self, login, password, email, validation_key="", type="U"):
        self.login = login
        self.password = password
        self.validation_key = validation_key
        self.email = email